package admin

import (
	"github.com/gin-gonic/gin"
	"go-ecommerce/internal/service"
	"go-ecommerce/internal/utils"
	"go-ecommerce/internal/validate"
	"gorm.io/gorm"
	"net/http"
	"strconv"
)

// AssignPermissionsHandler 给角色分配权限
func AssignPermissionsHandler(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		var req validate.AssignPermissionRequest
		if err := c.ShouldBindJSON(&req); err != nil {
			utils.Error(c, http.StatusBadRequest, err.Error())
			return
		}
		if err := service.AssignPermissions(db, req.RoleID, req.PermissionIDs); err != nil {
			utils.Error(c, http.StatusInternalServerError, err.Error())
			return
		}
		utils.Success(c, "权限绑定成功")
	}
}

// GetRolePermissionsHandler 获取角色已绑定权限ID列表
func GetRolePermissionsHandler(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		roleID, _ := strconv.ParseUint(c.Param("role_id"), 10, 64)
		ids, err := service.GetRolePermissionIDs(db, roleID)
		if err != nil {
			utils.Error(c, http.StatusInternalServerError, err.Error())
			return
		}
		utils.Success(c, "查询成功", ids)
	}
}
